package WebTools::AreaCode;
use strict;
use warnings;
use utf8;

use base qw/WebCore/;
#use Data::FormValidator::Constraints qw(ip_address);
use Encode;
use WebModule::City;
use WebModule::County;
use WebModule::Province;
use WebModule::City::Manager;
use WebModule::County::Manager;
use WebModule::Province::Manager;

#定义每页显示15条记录
my $per_page_number = 15;


#默认页
sub home:Default {#{{{
    my $this = shift;

    $this->set_tt_var('customer_title', '区号&邮政编码查询 - 5anet.com');
    return $this->process_template('area.tt2');

}#}}}


#按照地名查询邮政编码
sub findbyarea:Path('findbyarea') {#{{{
    my $this = shift;
    my $area = $this->query->param('area');

    if ( !$area) {
        return $this->home;
    }

    my $cities = WebModule::City::Manager->get_city(
        query=>[
            city=>{ like=>$area.'%'},
        ],
        sort_by=>'id',
    );


    my $counties = WebModule::County::Manager->get_county(
        query=>[
            county=>{ like=>$area.'%' },
        ],
        
        sort_by=>'id',
    );

    $this->set_tt_var('customer_title', '按地名:'.decode('utf-8', $area)
        .'查询邮政区号&邮政编码 - 5anet.com');
    $this->set_tt_var('area', decode('utf-8', $area));
    $this->set_tt_var('cities', $cities);
    $this->set_tt_var('counties', $counties);
    return $this->process_template('arearesult.tt2');
}#}}}

#查看城市详细的区县
sub citydetail:Regex('citydetail/(\d+)/(\d+)') {#{{{
    my $this = shift;
    my ($city_id, $page) = $this->action_args;

    if ( !$page ) {
        $page = 1;
    }

    my $city = WebModule::City->new(id=>$city_id);
    $city->load(speculative=>1);


    my $counties = WebModule::County::Manager->get_county(
        query=>[
            city=>$city_id,
        ],
        per_page=>$per_page_number,
        page=>$page,
        sort_by=>'id',
    );


    $this->set_tt_var('customer_title', $city->city
        .'所有区县区号&邮政编码 - 5anet.com');
    $this->set_tt_var('cityid', $city_id);
    $this->set_tt_var('counties', $counties);
    $this->set_tt_var('cityname', $city->city);
    $this->set_tt_var('pre_page', $page - 1 );
    $this->set_tt_var('next_page', $page + 1);
    return $this->process_template('arearesultcity.tt2');
}#}}}


#根据区号查询地名
sub findplacebyareacode:Path('findplacebyareacode') {#{{{
    my $this = shift;
    my $area_code = $this->query->param('areacode');

    if ( !$area_code ) {
        return $this->home;
    }

    #页面
    my $page = $this->query->param('page');
    if ( !$page ) {
        $page = 1;
    }

    #首先是市
    my $cities = WebModule::City::Manager->get_city(
        query=>[
        areacode=>{ like=>$area_code.'%' },
        ],
        per_page=>5,
        page=>$page,
        sort_by=>'id',
    );

    #再是县区等
    my $counties = WebModule::County::Manager->get_county(
        query=>[
            areacode=>{ like=>$area_code.'%'},
        ],

        per_page=>$per_page_number-5,
        page=>$page,
        sort_by=>'id',
    );



    $this->set_tt_var('customer_title', 
        '根据区号'.$area_code.'查询地名 - 5anet.com');
    $this->set_tt_var('pre_page', $page - 1 );
    $this->set_tt_var('next_page', $page + 1);
    $this->set_tt_var('counties', $counties);
    $this->set_tt_var('cities', $cities);
    $this->set_tt_var('areacode', $area_code);

    return $this->process_template('areacoderesult.tt2');


}#}}}

#根据邮政编码查询地名
sub findplacebyzipcode:Path('findplacebyzipcode') {#{{{
    my $this = shift;

    my $zip_code = $this->query->param('zipcode');

    if ( !$zip_code ) {
        return $this->home;
    }

    my $page = $this->query->param('page');
    if ( !$page ) {
        $page = 1;
    }

    #首先是市
    my $cities = WebModule::City::Manager->get_city(
        query=>[
        zipcode=>{ like=>$zip_code.'%' },
        ],
        per_page=>5,
        page=>$page,
        sort_by=>'id',
    );

    #再是县区等
    my $counties = WebModule::County::Manager->get_county(
        query=>[
            zipcode=>{ like=>$zip_code.'%'},
        ],

        per_page=>$per_page_number-5,
        page=>$page,
        sort_by=>'id',
    );



    $this->set_tt_var('customer_title', 
        '根据邮政编码'.$zip_code.'查询地名 - 5anet.com');
    $this->set_tt_var('pre_page', $page - 1 );
    $this->set_tt_var('next_page', $page + 1);
    $this->set_tt_var('counties', $counties);
    $this->set_tt_var('cities', $cities);
    $this->set_tt_var('zipcode', $zip_code);

    return $this->process_template('zipcoderesult.tt2');
}#}}}

#列出所有的省份
sub list:Path('list') {#{{{
    my $this = shift;

    my $provinces = WebModule::Province::Manager->get_province(
        sort_by=>'id'
    );

    $this->set_tt_var('customer_title', 
        '浏览邮政编码及区号- 5anet.com');
    $this->set_tt_var('provinces', $provinces);

    return $this->process_template('arealist.tt2');

}#}}}

#指定省份列出所有的城市
sub listcity:Regex('listcity/(\d+)') {
    my $this = shift;
    my $province_id = $this->action_args;

    my $cities = WebModule::City::Manager->get_city(
        query=>[
        province=>$province_id
        ],
        sort_by=>'id',
    );

    my $province = WebModule::Province->new(id=>$province_id);
    $province->load(speculative=>1);

    $this->set_tt_var('customer_title', 
        '浏览'.$province->province.'所有市县邮政编码及区号- 5anet.com');
    $this->set_tt_var('province', $province->province);
    $this->set_tt_var('cities', $cities);

    return $this->process_template('citylist.tt2');

}

1;
